﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;


namespace LCWMS
{
    public partial class w_so001_ld : DockContent
    {
        private int i_department = -1;
        private int i_reason = -1;
        private int i_checker = -1;
        private int i_applier = -1;

        private int i_flag = 0;//1:新增,2:修改

        //SQL语句
        private string s_sql_department;
        private string s_sql_reason;
        private string s_sql_applier;
        private string s_sql_checker;

        //DataSet
        private DataSet ds_department = new DataSet();
        private DataSet ds_reason = new DataSet();
        private DataSet ds_applier = new DataSet();
        private DataSet ds_checker = new DataSet();


        public w_so001_ld()
        {
            InitializeComponent();
        }

        #region 自定义函数
        /// <summary>
        /// 取数据库时间
        /// </summary>
        /// <returns></returns>
        private string f_get_time()
        {
            string s_sql;
            string s_return = "";
            switch (main.dbtype)
            {
                //系统权限管理
                case "SQL":
                    s_sql = "select getdate()";
                    s_return = main.dbservice.Sql_exec_selectstring(s_sql);
                    break;
                case "ORACLE":
                    s_sql = "select sysdate now from dual ";
                    s_return = main.dbservice.oracle_exec_selectstring(s_sql);
                    break;
                case "SYBASE":
                    break;
            }
            return s_return;
        }

        /// <summary>
        /// 界面按钮显示控制
        /// </summary>
        /// <param name="b">true：操作按钮可控制，保存取消按钮不可控制；false：反之</param>
        private void f_enabled(bool b)
        {
            btn_add_department.Enabled = b;
            btn_add_reason.Enabled = b;
            btn_add_checker.Enabled = b;
            btn_add_applier.Enabled = b;

            btn_modi_department.Enabled = b;
            btn_modi_reason.Enabled = b;
            btn_modi_checker.Enabled = b;
            btn_modi_applier.Enabled = b;

            btn_del_department.Enabled = b;
            btn_del_reason.Enabled = b;
            btn_del_checker.Enabled = b;
            btn_del_applier.Enabled = b;

            dgv_dept_class.Enabled = b;

            if (i_flag > 0)
            {
                btn_save_department.Enabled = !b;
                btn_save_reason.Enabled = !b;
                btn_save_checker.Enabled = !b;
                btn_save_applier.Enabled = !b;

                btn_cancel_department.Enabled = !b;
                btn_cancel_reason.Enabled = !b;
                btn_cancel_checker.Enabled = !b;
                btn_cancel_applier.Enabled = !b;
            }
            else if (i_flag == 0)
            {
                btn_save_department.Enabled = b;
                btn_save_reason.Enabled = b;
                btn_save_checker.Enabled = b;
                btn_save_applier.Enabled = b;

                btn_cancel_department.Enabled = b;
                btn_cancel_reason.Enabled = b;
                btn_cancel_checker.Enabled = b;
                btn_cancel_applier.Enabled = b;
            }
        }

        /// <summary>
        /// 绑定部门信息
        /// </summary>
        /// <param name="s_dept_class">部门类别</param>
        private void f_Bind_department(string s_dept_class)
        {
            if (!string.IsNullOrEmpty(s_dept_class))
            {
                s_sql_department = "select inv_org,dept_class,dept_code,dept_name,memo,main_dist,made_time,maker,update_time,updater from " + main.g_space + ".so_ld_department where inv_org='" + main.g_org + "' and dept_class='" + s_dept_class + "'";

                try
                {
                    if (main.dbtype == "SQL")
                        ds_department = main.dbservice.Sql_exec_selectsql(s_sql_department);
                    else
                        ds_department = main.dbservice.oracle_exec_selectsql(s_sql_department);

                    dgv_department.DataSource = ds_department.Tables[0];
                }
                catch (Exception ex)
                {
                    MessageBox.Show("获取部门信息失败!" + ex.Message);
                    return;
                }
                //设置主键
                ds_department.Tables[0].PrimaryKey = new DataColumn[] { ds_department.Tables[0].Columns["inv_org"], ds_department.Tables[0].Columns["dept_class"], ds_department.Tables[0].Columns["dept_code"] };
            }
        }

        /// <summary>
        /// 绑定用途信息
        /// </summary>
        /// <param name="s_dept_class">部门类别</param>
        private void f_Bind_reason(string s_dept_class)
        {
            if (!string.IsNullOrEmpty(s_dept_class))
            {
                s_sql_reason = "select inv_org,dept_class,ld_reason,reason_dist,memo,main_dist,made_time,maker,update_time,updater,inv_dist from " + main.g_space + ".so_ld_reason where inv_org='" + main.g_org + "' and dept_class='" + s_dept_class + "'";

                try
                {
                    if (main.dbtype == "SQL")
                        ds_reason = main.dbservice.Sql_exec_selectsql(s_sql_reason);
                    else
                        ds_reason = main.dbservice.oracle_exec_selectsql(s_sql_reason);

                    dgv_reason.DataSource = ds_reason.Tables[0];
                }
                catch (Exception ex)  
                {
                    MessageBox.Show("获取用途信息失败!" + ex.Message);
                    return;
                }
                //设置主键
                ds_reason.Tables[0].PrimaryKey = new DataColumn[] { ds_reason.Tables[0].Columns["inv_org"], ds_reason.Tables[0].Columns["dept_class"], ds_reason.Tables[0].Columns["ld_reason"], ds_reason.Tables[0].Columns["reason_dist"] };
            }
        }

        /// <summary>
        /// 绑定申请人信息
        /// </summary>
        /// <param name="s_dept_class">部门类别</param>
        private void f_Bind_applier(string s_dept_class)
        {
            if (!string.IsNullOrEmpty(s_dept_class))
            {
                s_sql_applier = "select inv_org,dept_class,applier_id,applier,memo,main_dist,made_time,maker,update_time,updater from " + main.g_space + ".so_ld_applier where inv_org='" + main.g_org + "' and dept_class='" + s_dept_class + "'";

                try
                {
                    if (main.dbtype == "SQL")
                        ds_applier = main.dbservice.Sql_exec_selectsql(s_sql_applier);
                    else
                        ds_applier = main.dbservice.oracle_exec_selectsql(s_sql_applier);

                    dgv_applier.DataSource = ds_applier.Tables[0];
                }
                catch (Exception ex)
                {
                    MessageBox.Show("获取申请人信息失败!" + ex.Message);
                    return;
                }
                //设置主键
                ds_applier.Tables[0].PrimaryKey = new DataColumn[] { ds_applier.Tables[0].Columns["inv_org"], ds_applier.Tables[0].Columns["dept_class"], ds_applier.Tables[0].Columns["applier_id"] };
            }
        }

        /// <summary>
        /// 绑定审批人信息
        /// </summary>
        /// <param name="s_dept_class">部门类别</param>
        private void f_Bind_checker(string s_dept_class)
        {
            if (!string.IsNullOrEmpty(s_dept_class))
            {
                s_sql_checker = "select inv_org,dept_class,checker_id,checker,memo,main_dist,made_time,maker,update_time,updater from " + main.g_space + ".so_ld_checker where inv_org='" + main.g_org + "' and dept_class='" + s_dept_class + "'";

                try
                {
                    if (main.dbtype == "SQL")
                        ds_checker = main.dbservice.Sql_exec_selectsql(s_sql_checker);
                    else
                        ds_checker = main.dbservice.oracle_exec_selectsql(s_sql_checker);

                    dgv_checker.DataSource = ds_checker.Tables[0];
                }
                catch (Exception ex)
                {
                    MessageBox.Show("获取审批人信息失败!" + ex.Message);
                    return;
                }
                //设置主键
                ds_checker.Tables[0].PrimaryKey = new DataColumn[] { ds_checker.Tables[0].Columns["inv_org"], ds_checker.Tables[0].Columns["dept_class"], ds_checker.Tables[0].Columns["checker_id"] };
            }
        }
        #endregion

        private void w_so001_ld_Load(object sender, EventArgs e)
        {
            //用途下拉框绑定
            DataTable dt_reason = new DataTable();
            dt_reason.Columns.Add("Value");
            dt_reason.Columns.Add("Name");
            DataRow dr_1 = dt_reason.NewRow();
            DataRow dr_2 = dt_reason.NewRow();

            dr_1["Value"] = "0";
            dr_1["Name"] = "申请";
            dr_2["Value"] = "1";
            dr_2["Name"] = "退料";
            dt_reason.Rows.Add(dr_1);
            dt_reason.Rows.Add(dr_2);

            DataGridViewComboBoxColumn dgvComboBoxColumn = dgv_reason.Columns["reason_dist2"] as DataGridViewComboBoxColumn;
            dgvComboBoxColumn.DataPropertyName = "reason_dist";
            dgvComboBoxColumn.DataSource = dt_reason;
            dgvComboBoxColumn.DisplayMember = "Name";
            dgvComboBoxColumn.ValueMember = "Value";

            //部门类别固定值添加
            DataTable dt = new DataTable();
           
            dt.Columns.Add("dept_class");
            DataRow dr = dt.NewRow();
            dr["dept_class"] = "车间";
            DataRow dr1 = dt.NewRow();
            dr1["dept_class"] = "公司部门";
            DataRow dr2 = dt.NewRow();
            dr2["dept_class"] = "供应商";
            dt.Rows.Add(dr);
            dt.Rows.Add(dr1);
            dt.Rows.Add(dr2);
            dgv_dept_class.DataSource = dt;

            DataTable dt2 = new DataTable();
            dt2.Columns.Add("Name");
            dt2.Columns.Add("Value");
            DataRow drr = dt2.NewRow();
            drr["Name"] = "是";
            drr["Value"] = "1";
            dt2.Rows.Add(drr);
            DataRow drr2 = dt2.NewRow();
            drr2["Name"] = "否";
            drr2["Value"] = "0";
            dt2.Rows.Add(drr2);
            DataGridViewComboBoxColumn dgvComboBoxColumn2 = dgv_reason.Columns["inv_dist"] as DataGridViewComboBoxColumn;
            dgvComboBoxColumn2.DataPropertyName = "inv_dist";
            dgvComboBoxColumn2.DataSource = dt2;
            dgvComboBoxColumn2.DisplayMember = "Name";
            dgvComboBoxColumn2.ValueMember = "Value";

            //语言转换
            main.g_class.comm_changeControl(this, main.g_win_dataset);
        }

        #region 部门信息操作
        private void btn_add_department_Click(object sender, EventArgs e)
        {
            if (dgv_dept_class.CurrentRow != null)
            {
                f_enabled(false);
                btn_save_department.Enabled = true;
                btn_cancel_department.Enabled = true;
                i_department = dgv_department.Rows.Count - 1;
                i_flag = 1;
                dgv_department.AllowUserToAddRows = true;

                ////光标显示到新增行首列
                //dgv_department.CurrentCell = dgv_department["dept_code1", dgv_department.RowCount - 1];
                //dgv_department.BeginEdit(true);
            }
        }

        private void btn_modi_department_Click(object sender, EventArgs e)
        {
            f_enabled(false);
            btn_save_department.Enabled = true;
            btn_cancel_department.Enabled = true;
            i_flag = 2;
        }

        private void btn_del_department_Click(object sender, EventArgs e)
        {
            if (dgv_department.CurrentRow != null)
            { 
                string s_dept_code=dgv_department.CurrentRow.Cells["dept_code1"].Value.ToString().Trim();
                DialogResult result;
                result = MessageBox.Show("确认删除部门" + s_dept_code + "?", "提示", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    string s_sql = "delete " + main.g_space + ".so_ld_department where inv_org='" + main.g_org + "' and dept_class='" + dgv_department.CurrentRow.Cells["dept_class1"].Value.ToString().Trim() + "' and dept_code='" + dgv_department.CurrentRow.Cells["dept_code1"].Value.ToString().Trim() + "'";
                    try
                    {
                        if (main.dbtype == "SQL")
                            main.dbservice.Sql_exec_commandsql(s_sql);
                        else
                            main.dbservice.oracle_exec_commandsql(s_sql);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("删除部门信息失败!" + ex.Message);
                        return;
                    }
                    f_Bind_department(dgv_dept_class.CurrentRow.Cells["dept_class"].Value.ToString().Trim());
                }
            }
        }

        private void btn_save_department_Click(object sender, EventArgs e)
        {
            if (dgv_dept_class.CurrentRow != null)
            {
                //新增
                if (i_flag == 1)
                {

                    //加时间
                    for (int i = 0; i < dgv_department.Rows.Count - 1; i++)
                    {
                        string s_time = f_get_time();
                        if (!string.IsNullOrEmpty(dgv_department.Rows[i].Cells["dept_code1"].Value.ToString().Trim()))
                        {
                            dgv_department.Rows[i].Cells["made_time1"].Value = s_time;
                        }
                        else
                        {
                            MessageBox.Show("部门编码不能为空!");
                            return;
                        }
                    }

                    try
                    {
                        if (main.dbtype == "SQL")
                        {
                            main.dbservice.Sql_refresh_db(ds_department, s_sql_department);
                        }
                        else
                        {
                            main.dbservice.oracle_refresh_db(ds_department, s_sql_department);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("部门信息保存失败!" + ex.Message);
                        return;
                    }
                }
                //更新
                else if (i_flag == 2)
                {
                    if (dgv_department.CurrentRow != null)
                    {
                        //加时间
                        for (int i = 0; i < dgv_department.Rows.Count - 1; i++)
                        {
                            string s_time = f_get_time();
                            if (!string.IsNullOrEmpty(dgv_department.Rows[i].Cells["dept_code1"].Value.ToString().Trim()))
                            {
                                dgv_department.Rows[i].Cells["update_time1"].Value = s_time;
                            }
                            else
                            {
                                MessageBox.Show("用途编码不能为空!");
                                return;
                            }
                        }
                        try
                        {
                            if (main.dbtype == "SQL")
                            {
                                main.dbservice.Sql_refresh_db(ds_department, s_sql_department);
                            }
                            else
                            {
                                main.dbservice.oracle_refresh_db(ds_department, s_sql_department);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("部门信息保存失败!" + ex.Message);
                            return;
                        }
                    }
                }
                f_Bind_department(dgv_dept_class.CurrentRow.Cells["dept_class"].Value.ToString().Trim());
                f_enabled(true);
                dgv_department.AllowUserToAddRows = false;
                dgv_department.EditMode = DataGridViewEditMode.EditProgrammatically;
                i_flag = 0;
            }
        }

        private void btn_cancel_department_Click(object sender, EventArgs e)
        {
            f_Bind_department(dgv_dept_class.CurrentRow.Cells["dept_class"].Value.ToString().Trim());
            f_enabled(true);
            dgv_department.AllowUserToAddRows = false;
            dgv_department.EditMode = DataGridViewEditMode.EditProgrammatically;
            i_flag = 0;
        }

        private void dgv_department_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                //新增
                if (i_flag == 1)
                {
                    //判断是否新增行
                    if (e.RowIndex > i_department)
                    {
                        dgv_department.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
                    }
                    else
                    {
                        dgv_department.EditMode = DataGridViewEditMode.EditProgrammatically;
                    }
                }
                //修改
                else if (i_flag == 2)
                {
                    if (e.ColumnIndex != 2)
                    {
                        dgv_department.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
                    }
                    else
                    {
                        dgv_department.EditMode = DataGridViewEditMode.EditProgrammatically;
                    }
                }
            }
        }

        private void dgv_department_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            //给新增行赋值
            dgv_department.Rows[e.Row.Index - 1].Cells["inv_org1"].Value = main.g_org;
            dgv_department.Rows[e.Row.Index - 1].Cells["dept_class1"].Value = dgv_dept_class.CurrentRow.Cells["dept_class"].Value;
            dgv_department.Rows[e.Row.Index - 1].Cells["main_dist1"].Value = "A";
            dgv_department.Rows[e.Row.Index - 1].Cells["maker1"].Value = main.g_user;
        }

        private void dgv_department_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //修改mian_dist与修改人
            dgv_department.CurrentRow.Cells["main_dist1"].Value = "C";
            dgv_department.CurrentRow.Cells["updater1"].Value = main.g_user;
        }
        #endregion

        #region 用途信息
        private void btn_add_reason_Click(object sender, EventArgs e)
        {
            if (dgv_dept_class.CurrentRow != null)
            {
                f_enabled(false);
                btn_save_reason.Enabled = true;
                btn_cancel_reason.Enabled = true;
                i_reason = dgv_reason.Rows.Count - 1;
                i_flag = 1;
                dgv_reason.AllowUserToAddRows = true;
                ////光标显示到新增行首列
                //dgv_reason.CurrentCell = dgv_reason["ld_reason2", dgv_reason.RowCount - 1];
                //dgv_reason.BeginEdit(true);

            }
        }

        private void btn_modi_reason_Click(object sender, EventArgs e)
        {
            f_enabled(false);
            btn_save_reason.Enabled = true;
            btn_cancel_reason.Enabled = true;
            i_flag = 2;
        }

        private void btn_del_reason_Click(object sender, EventArgs e)
        {
            if (dgv_reason.CurrentRow != null)
            {
                string s_ld_reason = dgv_reason.CurrentRow.Cells["ld_reason2"].Value.ToString().Trim();
                DialogResult result;
                result = MessageBox.Show("确认删除用途" + s_ld_reason + "?", "提示", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    string s_sql = "delete " + main.g_space + ".so_ld_reason where inv_org='" + main.g_org + "' and dept_class='" + dgv_reason.CurrentRow.Cells["dept_class2"].Value.ToString().Trim() + "' and ld_reason='" + dgv_reason.CurrentRow.Cells["ld_reason2"].Value.ToString().Trim() + "'";
                    try
                    {
                        if (main.dbtype == "SQL")
                            main.dbservice.Sql_exec_commandsql(s_sql);
                        else
                            main.dbservice.oracle_exec_commandsql(s_sql);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("删除用途信息失败!" + ex.Message);
                        return;
                    }
                    f_Bind_reason(dgv_dept_class.CurrentRow.Cells["dept_class"].Value.ToString().Trim());
                }
            }
        }

        private void btn_save_reason_Click(object sender, EventArgs e)
        {
            if (dgv_dept_class.CurrentRow != null)
            {
                int i_row = 0;
                bool be = false;
                for (int i = 0; i < dgv_reason.Rows.Count-1; i++)
                {
                    if (dgv_reason.Rows[i].Cells["inv_dist"].Value.ToString().Trim() == "")
                    {
                        MessageBox.Show("必须选择是否记录/扣除库存！");
                        be = true;
                        i_row = i;
                        break;
                    }
                }
                if (be)
                {
                    for (int i = 0; i < dgv_reason.Rows.Count; i++)
                    {
                        dgv_reason.Rows[i].Selected = false; 
                    }
                    dgv_reason.Rows[i_row].Selected = true;
                    return;
                }
                //新增
                if (i_flag == 1)
                {
                    //加时间
                    for (int i = 0; i < dgv_reason.Rows.Count - 1; i++)
                    {
                        string s_time = f_get_time();
                        if (!string.IsNullOrEmpty(dgv_reason.Rows[i].Cells["ld_reason2"].Value.ToString().Trim()))
                        {
                            dgv_reason.Rows[i].Cells["made_time2"].Value = s_time;
                        }
                        else
                        {
                            MessageBox.Show("用途编码不能为空!");
                            return;
                        }
                    }
                    try
                    {
                        if (main.dbtype == "SQL")
                        {
                            main.dbservice.Sql_refresh_db(ds_reason, s_sql_reason);
                        }
                        else
                        {
                            main.dbservice.oracle_refresh_db(ds_reason, s_sql_reason);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("用途信息保存失败!" + ex.Message);
                        return;
                    }
                }
                //更新
                else if (i_flag == 2)
                {
                    if (dgv_reason.CurrentRow != null)
                    {
                        //加时间
                        for (int i = 0; i < dgv_reason.Rows.Count - 1; i++)
                        {
                            string s_time = f_get_time();
                            if (!string.IsNullOrEmpty(dgv_reason.Rows[i].Cells["ld_reason2"].Value.ToString().Trim()))
                            {
                                dgv_reason.Rows[i].Cells["update_time2"].Value = s_time;
                            }
                            else
                            {
                                MessageBox.Show("用途编码不能为空!");
                                return;
                            }
                        }
                        try
                        {
                            if (main.dbtype == "SQL")
                            {
                                main.dbservice.Sql_refresh_db(ds_reason, s_sql_reason);
                            }
                            else
                            {
                                main.dbservice.oracle_refresh_db(ds_reason, s_sql_reason);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("用途信息保存失败!" + ex.Message);
                            return;
                        }
                    }
                }
                f_Bind_reason(dgv_dept_class.CurrentRow.Cells["dept_class"].Value.ToString().Trim());
                f_enabled(true);
                dgv_reason.AllowUserToAddRows = false;
                dgv_reason.EditMode = DataGridViewEditMode.EditProgrammatically;
                i_flag = 0;
            }
        }

        private void btn_cancel_reason_Click(object sender, EventArgs e)
        {
            f_Bind_reason(dgv_dept_class.CurrentRow.Cells["dept_class"].Value.ToString().Trim());
            f_enabled(true);
            dgv_reason.AllowUserToAddRows = false;
            dgv_reason.EditMode = DataGridViewEditMode.EditProgrammatically;
            i_flag = 0;
        }

        private void dgv_reason_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                //新增
                if (i_flag == 1)
                {
                    //判断是否新增行
                    if (e.RowIndex > i_reason)
                    {
                        dgv_reason.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
                    }
                    else
                    {
                        dgv_reason.EditMode = DataGridViewEditMode.EditProgrammatically;
                    }
                }
                //修改
                else if (i_flag == 2)
                {
                    if (e.ColumnIndex != 2)
                    {
                        dgv_reason.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
                    }
                    else
                    {
                        dgv_reason.EditMode = DataGridViewEditMode.EditProgrammatically;
                    }
                }
            }
        }

        private void dgv_reason_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            //给新增行赋值
            dgv_reason.Rows[e.Row.Index - 1].Cells["inv_org2"].Value = main.g_org;
            dgv_reason.Rows[e.Row.Index - 1].Cells["dept_class2"].Value = dgv_dept_class.CurrentRow.Cells["dept_class"].Value;
            dgv_reason.Rows[e.Row.Index - 1].Cells["main_dist2"].Value = "A";
            dgv_reason.Rows[e.Row.Index - 1].Cells["maker2"].Value = main.g_user;
        }

        private void dgv_reason_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //修改mian_dist与修改人
            dgv_reason.CurrentRow.Cells["main_dist2"].Value = "C";
            dgv_reason.CurrentRow.Cells["updater2"].Value = main.g_user;
        }
        #endregion

        #region 申请人
        private void btn_add_applier_Click(object sender, EventArgs e)
        {
            if (dgv_dept_class.CurrentRow != null)
            {
                f_enabled(false);
                btn_save_applier.Enabled = true;
                btn_cancel_applier.Enabled = true;
                i_applier = dgv_applier.Rows.Count - 1;
                i_flag = 1;
                dgv_applier.AllowUserToAddRows = true;

                ////光标显示到新增行首列
                //dgv_applier.CurrentCell = dgv_applier["applier_id3", dgv_applier.RowCount - 1];
                //dgv_applier.BeginEdit(true);

            }
        }

        private void btn_modi_applier_Click(object sender, EventArgs e)
        {
            f_enabled(false);
            btn_save_applier.Enabled = true;
            btn_cancel_applier.Enabled = true;
            i_flag = 2;
        }

        private void btn_del_applier_Click(object sender, EventArgs e)
        {
            if (dgv_applier.CurrentRow != null)
            {
                string s_dept_code = dgv_applier.CurrentRow.Cells["applier_id3"].Value.ToString().Trim();
                DialogResult result;
                result = MessageBox.Show("确认删除审批人" + s_dept_code + "?", "提示", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    string s_sql = "delete " + main.g_space + ".so_ld_applier where inv_org='" + main.g_org + "' and dept_class='" + dgv_applier.CurrentRow.Cells["dept_class3"].Value.ToString().Trim() + "' and applier_id='" + dgv_applier.CurrentRow.Cells["applier_id3"].Value.ToString().Trim() + "'";
                    try
                    {
                        if (main.dbtype == "SQL")
                            main.dbservice.Sql_exec_commandsql(s_sql);
                        else
                            main.dbservice.oracle_exec_commandsql(s_sql);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("删除审批人信息失败!" + ex.Message);
                        return;
                    }
                    f_Bind_applier(dgv_dept_class.CurrentRow.Cells["dept_class"].Value.ToString().Trim());
                }
            }
        }

        private void btn_save_applier_Click(object sender, EventArgs e)
        {
            if (dgv_dept_class.CurrentRow != null)
            {
                //新增
                if (i_flag == 1)
                {
                    //加时间
                    for (int i = 0; i < dgv_applier.Rows.Count - 1; i++)
                    {
                        string s_time = f_get_time();
                        if (!string.IsNullOrEmpty(dgv_applier.Rows[i].Cells["applier_id3"].Value.ToString().Trim()))
                        {
                            dgv_applier.Rows[i].Cells["made_time3"].Value = s_time;
                        }
                        else
                        {
                            MessageBox.Show("审批人编码不能为空!");
                            return;
                        }
                    }
                    try
                    {
                        if (main.dbtype == "SQL")
                        {
                            main.dbservice.Sql_refresh_db(ds_applier, s_sql_applier);
                        }
                        else
                        {
                            main.dbservice.oracle_refresh_db(ds_applier, s_sql_applier);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("审批人信息保存失败!" + ex.Message);
                        return;
                    }
                }
                //更新
                else if (i_flag == 2)
                {
                    if (dgv_applier.CurrentRow != null)
                    {
                        //加时间
                        for (int i = 0; i < dgv_applier.Rows.Count - 1; i++)
                        {
                            string s_time = f_get_time();
                            if (!string.IsNullOrEmpty(dgv_applier.Rows[i].Cells["applier_id3"].Value.ToString().Trim()))
                            {
                                dgv_applier.Rows[i].Cells["update_time3"].Value = s_time;
                            }
                            else
                            {
                                MessageBox.Show("用途编码不能为空!");
                                return;
                            }
                        }
                        try
                        {
                            if (main.dbtype == "SQL")
                            {
                                main.dbservice.Sql_refresh_db(ds_applier, s_sql_applier);
                            }
                            else
                            {
                                main.dbservice.oracle_refresh_db(ds_applier, s_sql_applier);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("审批人信息保存失败!" + ex.Message);
                            return;
                        }
                    }
                }
                f_Bind_applier(dgv_dept_class.CurrentRow.Cells["dept_class"].Value.ToString().Trim());
                f_enabled(true);
                dgv_applier.AllowUserToAddRows = false;
                dgv_applier.EditMode = DataGridViewEditMode.EditProgrammatically;
                i_flag = 0;
            }
        }

        private void btn_cancel_applier_Click(object sender, EventArgs e)
        {
            dgv_applier.AllowUserToAddRows = false;
            f_Bind_applier(dgv_dept_class.CurrentRow.Cells["dept_class"].Value.ToString().Trim());
            f_enabled(true);
            dgv_applier.EditMode = DataGridViewEditMode.EditProgrammatically;
            i_flag = 0;
        }

        private void dgv_applier_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                //新增
                if (i_flag == 1)
                {
                    //判断是否新增行
                    if (e.RowIndex > i_applier)
                    {
                        dgv_applier.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
                    }
                    else
                    {
                        dgv_applier.EditMode = DataGridViewEditMode.EditProgrammatically;
                    }
                }
                //修改
                else if (i_flag == 2)
                {
                    if (e.ColumnIndex != 2)
                    {
                        dgv_applier.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
                    }
                    else
                    {
                        dgv_applier.EditMode = DataGridViewEditMode.EditProgrammatically;
                    }
                }
            }
        }

        private void dgv_applier_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            //给新增行赋值
            dgv_applier.Rows[e.Row.Index - 1].Cells["inv_org3"].Value = main.g_org;
            dgv_applier.Rows[e.Row.Index - 1].Cells["dept_class3"].Value = dgv_dept_class.CurrentRow.Cells["dept_class"].Value;
            dgv_applier.Rows[e.Row.Index - 1].Cells["main_dist3"].Value = "A";
            dgv_applier.Rows[e.Row.Index - 1].Cells["maker3"].Value = main.g_user;
        }

        private void dgv_applier_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //修改mian_dist与修改人
            dgv_applier.CurrentRow.Cells["main_dist3"].Value = "C";
            dgv_applier.CurrentRow.Cells["updater3"].Value = main.g_user;
        }
        #endregion

        #region 审批人
        private void btn_add_checker_Click(object sender, EventArgs e)
        {
            if (dgv_dept_class.CurrentRow != null)
            {
                f_enabled(false);
                btn_save_checker.Enabled = true;
                btn_cancel_checker.Enabled = true;
                i_checker = dgv_checker.Rows.Count - 1;
                i_flag = 1;
                dgv_checker.AllowUserToAddRows = true;

                ////光标显示到新增行首列
                //dgv_checker.CurrentCell = dgv_checker["checker_id4", dgv_checker.RowCount - 1];
                //dgv_checker.BeginEdit(true);

            }
        }

        private void btn_modi_checker_Click(object sender, EventArgs e)
        {
            f_enabled(false);
            btn_save_checker.Enabled = true;
            btn_cancel_checker.Enabled = true;
            i_flag = 2;
        }

        private void btn_del_checker_Click(object sender, EventArgs e)
        {
            if (dgv_checker.CurrentRow != null)
            {
                string s_dept_code = dgv_checker.CurrentRow.Cells["checker_id4"].Value.ToString().Trim();
                DialogResult result;
                result = MessageBox.Show("确认删除申请人" + s_dept_code + "?", "提示", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    string s_sql = "delete " + main.g_space + ".so_ld_checker where inv_org='" + main.g_org + "' and dept_class='" + dgv_checker.CurrentRow.Cells["dept_class4"].Value.ToString().Trim() + "' and checker_id='" + dgv_checker.CurrentRow.Cells["checker_id4"].Value.ToString().Trim() + "'";
                    try
                    {
                        if (main.dbtype == "SQL")
                            main.dbservice.Sql_exec_commandsql(s_sql);
                        else
                            main.dbservice.oracle_exec_commandsql(s_sql);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("删除申请人信息失败!" + ex.Message);
                        return;
                    }
                    f_Bind_checker(dgv_dept_class.CurrentRow.Cells["dept_class"].Value.ToString().Trim());
                }
            }
        }

        private void btn_save_checker_Click(object sender, EventArgs e)
        {
            if (dgv_dept_class.CurrentRow != null)
            {
                //新增
                if (i_flag == 1)
                {
                    //加时间
                    for (int i = 0; i < dgv_checker.Rows.Count - 1; i++)
                    {
                        string s_time = f_get_time();
                        if (!string.IsNullOrEmpty(dgv_checker.Rows[i].Cells["checker_id4"].Value.ToString().Trim()))
                        {
                            dgv_checker.Rows[i].Cells["made_time4"].Value = s_time;
                        }
                        else
                        {
                            MessageBox.Show("申请人编码不能为空!");
                            return;
                        }
                    }
                    try
                    {
                        if (main.dbtype == "SQL")
                        {
                            main.dbservice.Sql_refresh_db(ds_checker, s_sql_checker);
                        }
                        else
                        {
                            main.dbservice.oracle_refresh_db(ds_checker, s_sql_checker);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("申请人信息保存失败!" + ex.Message);
                        return;
                    }
                }
                //更新
                else if (i_flag == 2)
                {
                    if (dgv_checker.CurrentRow != null)
                    {
                        //加时间
                        for (int i = 0; i < dgv_checker.Rows.Count - 1; i++)
                        {
                            string s_time = f_get_time();
                            if (!string.IsNullOrEmpty(dgv_checker.Rows[i].Cells["checker_id4"].Value.ToString().Trim()))
                            {
                                dgv_checker.Rows[i].Cells["update_time4"].Value = s_time;
                            }
                            else
                            {
                                MessageBox.Show("用途编码不能为空!");
                                return;
                            }
                        }
                        try
                        {
                            if (main.dbtype == "SQL")
                            {
                                main.dbservice.Sql_refresh_db(ds_checker, s_sql_checker);
                            }
                            else
                            {
                                main.dbservice.oracle_refresh_db(ds_checker, s_sql_checker);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("申请人信息保存失败!" + ex.Message);
                            return;
                        }
                    }
                }
                f_Bind_checker(dgv_dept_class.CurrentRow.Cells["dept_class"].Value.ToString().Trim());
                f_enabled(true);
                dgv_checker.AllowUserToAddRows = false;
                dgv_checker.EditMode = DataGridViewEditMode.EditProgrammatically;
                i_flag = 0;
            }
        }

        private void btn_cancel_checker_Click(object sender, EventArgs e)
        {
            f_Bind_checker(dgv_dept_class.CurrentRow.Cells["dept_class"].Value.ToString().Trim());
            f_enabled(true);
            dgv_checker.AllowUserToAddRows = false;
            dgv_checker.EditMode = DataGridViewEditMode.EditProgrammatically;
            i_flag = 0;
        }

        private void dgv_checker_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                //新增
                if (i_flag == 1)
                {
                    //判断是否新增行
                    if (e.RowIndex > i_checker)
                    {
                        dgv_checker.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
                    }
                    else
                    {
                        dgv_checker.EditMode = DataGridViewEditMode.EditProgrammatically;
                    }
                }
                //修改
                else if (i_flag == 2)
                {
                    if (e.ColumnIndex != 2)
                    {
                        dgv_checker.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
                    }
                    else
                    {
                        dgv_checker.EditMode = DataGridViewEditMode.EditProgrammatically;
                    }
                }
            }
        }

        private void dgv_checker_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            //给新增行赋值
            dgv_checker.Rows[e.Row.Index - 1].Cells["inv_org4"].Value = main.g_org;
            dgv_checker.Rows[e.Row.Index - 1].Cells["dept_class4"].Value = dgv_dept_class.CurrentRow.Cells["dept_class"].Value;
            dgv_checker.Rows[e.Row.Index - 1].Cells["main_dist4"].Value = "A";
            dgv_checker.Rows[e.Row.Index - 1].Cells["maker4"].Value = main.g_user;
        }

        private void dgv_checker_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //修改mian_dist与修改人
            dgv_checker.CurrentRow.Cells["main_dist4"].Value = "C";
            dgv_checker.CurrentRow.Cells["updater4"].Value = main.g_user;
        }
        #endregion

        private void dgv_dept_class_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dgv_dept_class.CurrentRow != null)
            {
                string s_dept_class = dgv_dept_class.CurrentRow.Cells["dept_class"].Value.ToString().Trim();
                f_Bind_department(s_dept_class);
                f_Bind_reason(s_dept_class);
                f_Bind_applier(s_dept_class);
                f_Bind_checker(s_dept_class);
            }
        }

        private void dgv_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            if (e.Exception != null)
            {
                if (e.Exception.GetType().FullName == "System.Data.ConstraintException")
                {
                    MessageBox.Show(e.Exception.Message);
                }
                else if (e.Exception.GetType().FullName == "System.Data.NoNullAllowedException")
                {
                    MessageBox.Show("第一列为必填项!");
                }

                e.Cancel = true;
            }
        }
    }
}
